package ming;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "LogoutServlet",urlPatterns="/LogoutServlet")
public class LogoutServlet extends HttpServlet {
    private static Map NameMap = new HashMap();
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException {

        ServletContext context = this.getServletContext();
        NameMap=(Map) context.getAttribute("NameMap");
        NameMap.remove(request.getSession().getId());

        context.setAttribute("NameMap",NameMap);
        context.setAttribute("Online",NameMap.size());
        // 将Session对象中的User对象移除
        request.getSession().removeAttribute("user");
        response.sendRedirect("/code_war_exploded/login");
    }
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)throws ServletException, IOException {
        doGet(request, response);
    }
}
